<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="赛项名称" prop="baseMatchItemId">
        <el-select v-model="dataForm.baseMatchItemId" placeholder="赛项名称" :rules="[{ required: true, message: '请选择赛项名称', trigger: 'blur' }]" @change="changeSelect">
          <el-option v-for="item in matchList" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="老师的数量" prop="teacherNum">
        <el-input v-model="dataForm.teacherNum" placeholder="" style="width: 100px"></el-input>
        ~<el-input v-model="dataForm.teacherNumMax" placeholder="" style="width: 100px"></el-input>
      </el-form-item>

      <el-form-item label="学生的数量" prop="studentNum">
        <el-input v-model="dataForm.studentNum" placeholder="" style="width: 100px"></el-input>
        ~<el-input v-model="dataForm.studentNumMax" placeholder="" style="width: 100px"></el-input>
      </el-form-item>
      <el-form-item label="赛项编号" prop="studentNum">
        <el-input v-model="dataForm.code" placeholder="" style="width: 100px"></el-input>
      </el-form-item>
      <el-form-item label="比赛场地" prop="studentNum">
        <el-input v-model="dataForm.addr" placeholder="" style="width: 100px"></el-input>
      </el-form-item>
      <el-form-item label="管理员" prop="adminId">
        <ren-user-selector v-model="dataForm.adminId" placeholder="请选择" v-model:adminName="dataForm.adminName" v-model:adminId="dataForm.adminId"></ren-user-selector>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
export default defineComponent({
  props: { dictTypeId: String },
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        matchId: "",
        baseMatchItemId: "",
        teacherNum: 1,
        teacherNumMax: 1,
        studentNum: 1,
        studentNumMax: 1,
        code: "",
        addr: "",
        adminId: "",
        adminName: ""
      },
      matchList: [
        {
          id: "",
          itemName: ""
        }
      ]
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataForm.matchId = this.dictTypeId as string;
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
    //this.getMatchItem();
  },
  mounted() {
    //this.getMatchItem();
  },

  watch: {
    dictTypeId() {
      this.dataForm.matchId = this.dictTypeId as string;
    }
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/sp/spmatchitem/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 查询赛项列表
    getMatchItem() {
      baseService.post("/sp/spbasematchitem/" + (this.dataForm.id ? "listAll" : "list"), this.dataForm).then((res: any) => {
        this.matchList = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      // baseMatchItemId 不能为空
      if (!this.dataForm.baseMatchItemId) {
        this.$message.error("请选择赛项名称");
        return;
      }

      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/sp/spmatchitem", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    },
    changeSelect() {
      baseService.get("/sp/spbasematchitem/" + this.dataForm.baseMatchItemId).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        } else {
          this.dataForm.teacherNum = res.data.teacherNum;
          this.dataForm.teacherNumMax = res.data.teacherNumMax;
          this.dataForm.studentNum = res.data.studentNum;
          this.dataForm.studentNumMax = res.data.studentNumMax;
        }
      });
    }
  }
});
</script>
